package com.maxsalad.uc.controller;

import com.maxsalad.common.message.MessageResult;
import com.maxsalad.pospal.entity.dish.list.ResponseDishListEntity;
import com.maxsalad.uc.pojo.ResponseResult;
import com.maxsalad.uc.pojo.TDishEntity;
import com.maxsalad.uc.pojo.TShopEntity;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@RequestMapping(value = "/dish")
@RestController
public class DishController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(DishController.class);
    @Autowired
    private com.maxsalad.uc.service.DishService dishService;

    @Autowired
    private com.maxsalad.pospal.service.DishService pospalDishService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseResult queryAllDish(@RequestParam Map paramMap) {
        try {
            TDishEntity dishEntity = new TDishEntity();
            dishEntity.setId(MapUtils.getLong(paramMap, "id", 0L));
            dishEntity.setShopId(MapUtils.getLong(paramMap, "shopId", 0L));
            dishEntity.setBarcode(MapUtils.getString(paramMap, "barcode"));
            dishEntity.setDishName(MapUtils.getString(paramMap, "dishName"));
            dishEntity.setDescription(MapUtils.getString(paramMap, "description"));

            Map map = dishService.queryAllDish(dishEntity);
            return getSucResultData(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseResult addDish(@RequestBody TDishEntity dishEntity) {
        try {
            MessageResult mr = dishService.saveOrUpdateDish(dishEntity);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseResult updateDish(@PathVariable long id, @RequestBody TDishEntity dishEntity) {
        try {
            dishEntity.setId(id);
            MessageResult mr = dishService.saveOrUpdateDish(dishEntity);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult deleteDish(@PathVariable long id) {
        try {
            MessageResult mr = dishService.deletePlatform(id);
            return getMessage(mr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }


    /**
     * 从银豹系统查询数据
     *
     * @param shopId
     * @return
     */
    @RequestMapping(value = "/shop/{shopId}", method = RequestMethod.GET)
    public ResponseResult queryAllPlatform(@PathVariable long shopId) {
        try {
            ResponseDishListEntity entity = pospalDishService.getAllDish(shopId);
            if ("success".equals(entity.getStatus())) {
                return getSucResultData(entity.getData());
            }
            List<String> list = entity.getMessages();
            if (list != null && list.size() > 0) {
                for (String item : list) {
                    if (StringUtils.isNotBlank(item)) {
                        log.error(item);
                    }
                }
            }
            return getFaultMessage();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

        return getFaultMessage();
    }
}
